package com.ruoyi.web.controller.house;

import com.ruoyi.area.domain.HouseAreaT;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.data.DataUtils;
import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.door.service.IHouseDoorTService;
import com.ruoyi.group.domain.HouseGroupT;
import com.ruoyi.group.service.IHouseGroupTService;
import com.ruoyi.purpose.domain.HousePurposeT;
import com.ruoyi.purpose.service.IHousePurposeTService;
import com.ruoyi.unit.domain.HouseUnitT;
import com.ruoyi.unit.service.IHouseUnitTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 楼宇管理Controller
 *
 * @author lijie
 * @date 2023-04-19
 */
@RestController
@RequestMapping("/house/group")
public class HouseGroupTController extends BaseController
{
    @Autowired
    private IHouseGroupTService houseGroupTService;
    @Autowired
    private IHouseUnitTService houseUnitTService;
    @Autowired
    private IHousePurposeTService housePurpopseTService;
    @Autowired
    private IHouseDoorTService houseDoorTService;
    /**
     * 查询楼宇管理列表
     */
     @GetMapping("/list")
    public TableDataInfo list(HouseGroupT houseGroupT)
    {
        startPage();
        HouseGroupT houseGroupVo1 = new HouseGroupT();
        houseGroupVo1.setAreaName(houseGroupT.getAreaName());
        houseGroupVo1.setGroupName(houseGroupT.getGroupName());
        houseGroupVo1.setCodeIn(houseGroupT.getCodeIn());
        houseGroupVo1.setCodeOut(houseGroupT.getCodeOut());
        houseGroupVo1.setSpaceSts(houseGroupT.getSpaceSts());
        HouseGroupT houseGroupVo2 = new HouseGroupT();
        List<HouseGroupT> list=new ArrayList<>();
        if ("宏宇富宸小区".equals(houseGroupT.getAreaName())){
             list = houseGroupTService.selectAreaGroupTList(houseGroupT);
        }else{
             list = houseGroupTService.selectHouseGroupTList(houseGroupT);
         }
        for(int i = 0;i<list.size();i++) {
            //总户数&总面积
            HouseGroupT houseGroupVO = new HouseGroupT();
            houseGroupT.setAreaName(list.get(i).getAreaName());
            houseGroupT.setGroupName(list.get(i).getGroupName());
            houseGroupVO = houseGroupTService.getDoorNumber(houseGroupT);
            String DoorNumber= String.valueOf(0d);
            DoorNumber=houseGroupVO.getDoorNumber();
            list.get(i).setDoorNumber(houseGroupVO.getDoorNumber());
            list.get(i).setSpaceNum(houseGroupVO.getSpaceNum());
            //应缴户数已缴户数未缴户数
            HouseGroupT groupVO = new HouseGroupT();
            houseGroupT.setAreaName(list.get(i).getAreaName());
            houseGroupT.setGroupName(list.get(i).getGroupName());
            houseGroupVO = houseGroupTService.getMust(houseGroupT);
            groupVO = houseGroupTService.getAlready(houseGroupT);
            list.get(i).setMustDoor(houseGroupVO.getMustDoor());
            list.get(i).setAlreadyDoor(groupVO.getAlreadyDoor());
            list.get(i).setNotDoor(houseGroupVO.getMustDoor()-groupVO.getAlreadyDoor());
            //已缴户数比率
            Double costRatio = 0d;
            if(!("0".equals(DoorNumber))){
                costRatio = DataUtils.doubleScale1(Double.valueOf(groupVO.getAlreadyDoor()) / Double.valueOf(DoorNumber)) * 100;
            }
            Integer costRatio1 = Integer.valueOf(costRatio.intValue());
            list.get(i).setCostRatio(String.valueOf(costRatio1) + "%");
            //已徼/未缴
            houseGroupT.setAreaName(list.get(i).getAreaName());
            houseGroupT.setGroupName(list.get(i).getGroupName());
            houseGroupVO = houseGroupTService.getGroupCost(houseGroupT);
            list.get(i).setBudgetMoney(houseGroupVO.getBudgetMoney());
            list.get(i).setCostMoney(houseGroupVO.getCostMoney());
            list.get(i).setCostMargin(DataUtils.costMargin(houseGroupVO.getBudgetMoney(),houseGroupVO.getCostMoney()));
        }
        if(getDataTableSumJudge(list)){
            HouseGroupT houseGroupVO = new HouseGroupT();
            houseGroupVO = houseGroupTService.getDoorNumber(houseGroupVo1);
            houseGroupVo2.setDoorNumber(houseGroupVO.getDoorNumber());
            houseGroupVo2.setSpaceNum(houseGroupVO.getSpaceNum());
            HouseGroupT groupVO = new HouseGroupT();
            houseGroupVO = houseGroupTService.getMust(houseGroupVo1);
            groupVO = houseGroupTService.getAlready(houseGroupVo1);
            houseGroupVo2.setMustDoor(houseGroupVO.getMustDoor());
            houseGroupVo2.setAlreadyDoor(groupVO.getAlreadyDoor());
            houseGroupVo2.setNotDoor(houseGroupVO.getMustDoor()-groupVO.getAlreadyDoor());
            houseGroupVO = houseGroupTService.getGroupCost(houseGroupVo1);
            houseGroupVo2.setBudgetMoney(houseGroupVO.getBudgetMoney());
            houseGroupVo2.setCostMoney(houseGroupVO.getCostMoney());
            houseGroupVo2.setCostMargin(DataUtils.costMargin(houseGroupVO.getBudgetMoney(),houseGroupVO.getCostMoney()));
            if(houseGroupVo2!=null){
                houseGroupVo2.setAreaName("合计");
            }
            list.add(houseGroupVo2);
        }
        return getDataTable(list);
    }

    /**
     * 导出楼宇管理列表
     */
    @Log(title = "楼宇管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseGroupT houseGroupT)
    {
        HouseGroupT houseGroupVo1 = new HouseGroupT();
        houseGroupVo1.setAreaName(houseGroupT.getAreaName());
        houseGroupVo1.setGroupName(houseGroupT.getGroupName());
        houseGroupVo1.setCodeIn(houseGroupT.getCodeIn());
        houseGroupVo1.setCodeOut(houseGroupT.getCodeOut());
        houseGroupVo1.setSpaceSts(houseGroupT.getSpaceSts());
        HouseGroupT houseGroupVo2 = new HouseGroupT();
        List<HouseGroupT> list = houseGroupTService.selectHouseGroupTList(houseGroupT);
        for(int i = 0;i<list.size();i++) {
            //总户数&总面积
            HouseGroupT houseGroupVO = new HouseGroupT();
            houseGroupT.setAreaName(list.get(i).getAreaName());
            houseGroupT.setGroupName(list.get(i).getGroupName());
            houseGroupVO = houseGroupTService.getDoorNumber(houseGroupT);
            String DoorNumber= String.valueOf(0d);
            DoorNumber=houseGroupVO.getDoorNumber();
            list.get(i).setDoorNumber(houseGroupVO.getDoorNumber());
            list.get(i).setSpaceNum(houseGroupVO.getSpaceNum());
            //应缴户数已缴户数未缴户数
            HouseGroupT groupVO = new HouseGroupT();
            houseGroupT.setAreaName(list.get(i).getAreaName());
            houseGroupT.setGroupName(list.get(i).getGroupName());
            houseGroupVO = houseGroupTService.getMust(houseGroupT);
            groupVO = houseGroupTService.getAlready(houseGroupT);
            list.get(i).setMustDoor(houseGroupVO.getMustDoor());
            list.get(i).setAlreadyDoor(groupVO.getAlreadyDoor());
            list.get(i).setNotDoor(houseGroupVO.getMustDoor()-groupVO.getAlreadyDoor());
            //已缴户数比率
            Double costRatio = 0d;
            if(!("0".equals(DoorNumber))){
                costRatio = DataUtils.doubleScale1(Double.valueOf(groupVO.getAlreadyDoor()) / Double.valueOf(DoorNumber)) * 100;
            }
            Integer costRatio1 = Integer.valueOf(costRatio.intValue());
            list.get(i).setCostRatio(String.valueOf(costRatio1) + "%");
            //已徼/未缴
            houseGroupT.setAreaName(list.get(i).getAreaName());
            houseGroupT.setGroupName(list.get(i).getGroupName());
            houseGroupVO = houseGroupTService.getGroupCost(houseGroupT);
            list.get(i).setBudgetMoney(houseGroupVO.getBudgetMoney());
            list.get(i).setCostMoney(houseGroupVO.getCostMoney());
            list.get(i).setCostMargin(DataUtils.costMargin(houseGroupVO.getBudgetMoney(),houseGroupVO.getCostMoney()));
        }
        if(getDataTableSumJudge(list)){
            HouseGroupT houseGroupVO = new HouseGroupT();
            houseGroupVO = houseGroupTService.getDoorNumber(houseGroupVo1);
            houseGroupVo2.setDoorNumber(houseGroupVO.getDoorNumber());
            houseGroupVo2.setSpaceNum(houseGroupVO.getSpaceNum());
            HouseGroupT groupVO = new HouseGroupT();
            houseGroupVO = houseGroupTService.getMust(houseGroupVo1);
            groupVO = houseGroupTService.getAlready(houseGroupVo1);
            houseGroupVo2.setMustDoor(houseGroupVO.getMustDoor());
            houseGroupVo2.setAlreadyDoor(groupVO.getAlreadyDoor());
            houseGroupVo2.setNotDoor(houseGroupVO.getMustDoor()-groupVO.getAlreadyDoor());
            houseGroupVO = houseGroupTService.getGroupCost(houseGroupVo1);
            houseGroupVo2.setBudgetMoney(houseGroupVO.getBudgetMoney());
            houseGroupVo2.setCostMoney(houseGroupVO.getCostMoney());
            houseGroupVo2.setCostMargin(DataUtils.costMargin(houseGroupVO.getBudgetMoney(),houseGroupVO.getCostMoney()));
            if(houseGroupVo2!=null){
                houseGroupVo2.setAreaName("合计");
            }
            list.add(houseGroupVo2);
        }
        ExcelUtil<HouseGroupT> util = new ExcelUtil<HouseGroupT>(HouseGroupT.class);
        util.exportExcel(response, list, "楼宇管理数据");
    }

    /**
     * 获取楼宇管理详细信息
     */
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable("groupId") Long groupId)
    {
        return AjaxResult.success(houseGroupTService.selectHouseGroupTByGroupId(groupId));
    }

    /**
     * 新增楼宇管理
     */
     @Log(title = "楼宇管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseGroupT houseGroupT)
    {
        houseGroupT.setCreater(getUsername());
        return toAjax(houseGroupTService.insertHouseGroupT(houseGroupT));
    }

    /**
     * 修改楼宇管理
     */
    @Log(title = "楼宇管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseGroupT houseGroupT)
    {
        houseGroupT.setCreater(getUsername());
        return toAjax(houseGroupTService.updateHouseGroupT(houseGroupT));
    }

    /**
     * 删除楼宇管理
     */
    @Log(title = "楼宇管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long[] groupIds)
    {
        HouseGroupT houseGroupVO=new HouseGroupT();
        HouseUnitT houseUnitVO=new HouseUnitT();
        for(Long groupId : groupIds){
            houseGroupVO = houseGroupTService.selectHouseGroupTByGroupId(groupId);
            houseUnitVO.setGroupName(houseGroupVO.getGroupName());
            houseUnitVO.setAreaName(houseGroupVO.getAreaName());
            List<HouseUnitT> list= new ArrayList<>();
            list =houseUnitTService.selectHouseUnitTByUnitName(houseUnitVO);
            for (int i=0;i< list.size();i++) {
                if ((list.get(i).getUnitName() != null)) {
                    return AjaxResult.error("存在单元,不允许删除");
                }
            }
        }
        return toAjax(houseGroupTService.deleteHouseGroupTByGroupIds(groupIds));
    }

    /**
     * 通过小区编号获取楼宇管理详细信息
     */
    @GetMapping(value = "/getGroupInfo/{areaName}")
    public TableDataInfo getGroupInfoByAreaId(@PathVariable("areaName") String areaName)
    {
        HouseGroupT houseGroupT = new HouseGroupT();
        houseGroupT.setAreaName(areaName);
        List<HouseGroupT> list=new ArrayList<>();
        if ("宏宇富宸小区".equals(houseGroupT.getAreaName())){
            list = houseGroupTService.selectAreaGroupTList(houseGroupT);
        }else {
            list = houseGroupTService.selectHouseGroupTList(houseGroupT);
        }
        return getDataTable(list);
    }


    @Log(title = "楼宇迁移管理", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/moveGroup")
    public AjaxResult moveGroup(@RequestBody HouseGroupT houseGroupT)
    {
//        HouseGroupT vo=houseGroupTService.selectGroup(houseGroupT);
        return toAjax(houseGroupTService.moveHouseGroupT(houseGroupT));
    }

    @Log(title = "楼宇用途修改管理", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/purposeGroup")
    public AjaxResult purposeGroup(@RequestBody HouseDoorT houseDoorT)
    {
        HousePurposeT housePurposeT = new HousePurposeT();
        HouseGroupT houseGroupT = new HouseGroupT();
        List<HouseDoorT> doorList = new ArrayList<HouseDoorT>();
        housePurposeT = housePurpopseTService.selectHousePurposeTByPurposeId(Long.valueOf(houseDoorT.getPurposeName()));
        houseGroupT = houseGroupTService.selectHouseGroupTByGroupId(houseDoorT.getGroupId());
        houseDoorT.setGroupName(houseGroupT.getGroupName());
        houseDoorT.setAreaName(houseGroupT.getAreaName());
        doorList = houseDoorTService.selectHouseDoorTList(houseDoorT);
        houseDoorT.setPurposeId(housePurposeT.getPurposeId());
        houseDoorT.setStandard(houseDoorT.getPrice());
        return toAjax(houseGroupTService.updateHouseGroupPurposeT(doorList,houseDoorT));
    }

}
